package com.ruoyi.examination.service.impl;

import java.sql.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.examination.domain.Projects;
import com.ruoyi.examination.domain.QuestionGroupRelation;
import com.ruoyi.examination.mapper.QuestionGroupRelationMapper;
import com.ruoyi.examination.util.SnowflakeIdUtils;
import com.ruoyi.examination.vo.QuestionGroupSelectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.examination.mapper.QuestionGroupMapper;
import com.ruoyi.examination.domain.QuestionGroup;
import com.ruoyi.examination.service.IQuestionGroupService;

import static com.ruoyi.common.core.utils.PageUtils.startPage;

/**
 * 题组管理Service业务层处理
 *
 * @author hcc
 * @date 2025-10-21
 */
@Service
public class QuestionGroupServiceImpl implements IQuestionGroupService
{
    @Autowired
    private QuestionGroupMapper questionGroupMapper;

    @Autowired
    private QuestionGroupRelationMapper questionGroupRelationMapper;
    /**
     * 查询题组管理
     *
     * @param groupId 题组管理主键
     * @return 题组管理
     */
    @Override
    public QuestionGroup selectQuestionGroupByGroupId(String groupId)
    {
        return questionGroupMapper.selectQuestionGroupByGroupId(groupId);
    }

    /**
     * 查询题组管理列表
     *
     * @param questionGroup 题组管理
     * @return 题组管理
     */
    @Override
    public List<QuestionGroup> selectQuestionGroupList(QuestionGroup questionGroup)
    {
        return questionGroupMapper.selectQuestionGroupList(questionGroup);
    }

    /**
     * 新增题组管理
     *
     * @param questionGroup 题组管理
     * @return 结果
     */
    @Override
    public int insertQuestionGroup(QuestionGroup questionGroup)
    {
        if (StringUtils.isNotEmpty(questionGroup.getTrainingProjectId())){
            Projects projects = questionGroupMapper.selectProjectName(questionGroup.getTrainingProjectId());
            if (StringUtils.isNotNull(projects)){
                questionGroup.setTrainingProjectName(projects.getExampleOne());
            }
        }
        questionGroup.setDelFlag("0");
        questionGroup.setRelateStatus("2");
        questionGroup.setGroupId(String.valueOf(new SnowflakeIdUtils(5,8).nextId()));
        questionGroup.setCreateBy(SecurityUtils.getUsername());
        questionGroup.setCreateTime(DateUtils.getNowDate());
        return questionGroupMapper.insertQuestionGroup(questionGroup);
    }

    /**
     * 修改题组管理
     *
     * @param questionGroup 题组管理
     * @return 结果
     */
    @Override
    public int updateQuestionGroup(QuestionGroup questionGroup)
    {
        if (StringUtils.isNotEmpty(questionGroup.getTrainingProjectId())){
            Projects projects = questionGroupMapper.selectProjectName(questionGroup.getTrainingProjectId());
            if (StringUtils.isNotNull(projects)){
                questionGroup.setTrainingProjectName(projects.getExampleOne());
            }
        }
        questionGroup.setUpdateTime(DateUtils.getNowDate());
        return questionGroupMapper.updateQuestionGroup(questionGroup);
    }

    /**
     * 批量删除题组管理
     *
     * @param groupIds 需要删除的题组管理主键
     * @return 结果
     */
    @Override
    public int deleteQuestionGroupByGroupIds(String[] groupIds)
    {
        return questionGroupMapper.deleteQuestionGroupByGroupIds(groupIds);
    }

    /**
     * 删除题组管理信息
     *
     * @param groupId 题组管理主键
     * @return 结果
     */
    @Override
    public int deleteQuestionGroupByGroupId(String groupId)
    {
        return questionGroupMapper.deleteQuestionGroupByGroupId(groupId);
    }

    /**
     * 批量关联问题
     * @param questionGroup
     * @return
     */
    @Override
    public int associationQuestion(QuestionGroup questionGroup) {
        if (StringUtils.isNotEmpty(questionGroup.getGroupId()) && StringUtils.isNotEmpty(questionGroup.getQuestionIds())){
            List<QuestionGroupRelation> questionGroupRelationList = questionGroupRelationMapper.selectQuestionGroupRelationByGroupId(questionGroup.getGroupId());
            List<String> collect = questionGroupRelationList.stream().map(QuestionGroupRelation::getQuestionId).collect(Collectors.toList());
            List<String> questionIdsList = new ArrayList<>(Arrays.asList(questionGroup.getQuestionIds()));
            // 移除已关联的问题
            questionIdsList.removeIf(questionId -> collect.contains(questionId));
            for (String questionId : questionIdsList) {
                QuestionGroupRelation questionGroupRelation = new QuestionGroupRelation();
                questionGroupRelation.setQuestionGroupId(String.valueOf(new SnowflakeIdUtils(5,8).nextId()));
                questionGroupRelation.setQuestionId(questionId);
                questionGroupRelation.setGroupId(questionGroup.getGroupId());
                questionGroupRelationMapper.insertQuestionGroupRelation(questionGroupRelation);
            }
        }
        int i = 0;
        List<QuestionGroupRelation> questionGroupRelationList = questionGroupRelationMapper.selectQuestionGroupRelationByGroupId(questionGroup.getGroupId());
        QuestionGroup questionGroupDb = questionGroupMapper.selectQuestionGroupByGroupId(questionGroup.getGroupId());
        if (StringUtils.isNotNull(questionGroupDb)){
            questionGroupDb.setRelateStatus("1");
            questionGroupDb.setQuestionCount(Long.valueOf(questionGroupRelationList.size()));
            questionGroupDb.setUpdateTime(DateUtils.getNowDate());
            questionGroupDb.setCreateBy(SecurityUtils.getUsername());
            i = questionGroupMapper.updateQuestionGroup(questionGroupDb);
        }
        return i;
    }


    /**
     * 获取题组列表
     * @param questionGroupSelectVo
     * @return
     */
    @Override
    public List<QuestionGroup> getList(QuestionGroupSelectVo questionGroupSelectVo) {

        if (StringUtils.isNotEmpty(questionGroupSelectVo.getQuestionId())){
            // 获取已关联题组id
            List<QuestionGroupRelation> questionGroupRelationList = questionGroupRelationMapper.selectQuestionGroupRelationByQuestionId(questionGroupSelectVo.getQuestionId());
            List<String> groupIds = questionGroupRelationList.stream()
                    .map(QuestionGroupRelation::getGroupId)
                    .filter(StringUtils::isNotEmpty)
                    .collect(Collectors.toList());
            questionGroupSelectVo.setGroupIds(groupIds);
            if (StringUtils.isNotEmpty(questionGroupSelectVo.getRelateStatus()) && "1".equals(questionGroupSelectVo.getRelateStatus())){
                questionGroupSelectVo.setRelateStatus("");
                if (questionGroupSelectVo.getPageNum() != null && questionGroupSelectVo.getPageSize() != null){
                    startPage(questionGroupSelectVo.getPageNum(), questionGroupSelectVo.getPageSize());
                }
                return questionGroupMapper.getAssociatedList(questionGroupSelectVo);
            }
        }
        if (questionGroupSelectVo.getPageNum() != null && questionGroupSelectVo.getPageSize() != null){
            startPage(questionGroupSelectVo.getPageNum(), questionGroupSelectVo.getPageSize());
        }
        return questionGroupMapper.getList(questionGroupSelectVo);
    }

    /**
     * 取消关联问题
     * @param questionGroup
     * @return
     */
    @Override
    public int cancelAssociationQuestion(QuestionGroup questionGroup) {
        int i = 0;
        if (StringUtils.isNotEmpty(questionGroup.getGroupId()) && StringUtils.isNotEmpty(questionGroup.getQuestionIds())){
            i = questionGroupRelationMapper.deleteQuestionGroupRelationByCondition(questionGroup.getGroupId(),questionGroup.getQuestionIds());
        }
        List<QuestionGroupRelation> questionGroupRelationList = questionGroupRelationMapper.selectQuestionGroupRelationByGroupId(questionGroup.getGroupId());
        QuestionGroup questionGroupDb = questionGroupMapper.selectQuestionGroupByGroupId(questionGroup.getGroupId());
        if (StringUtils.isNotNull(questionGroupDb)){
            questionGroupDb.setQuestionCount(Long.valueOf(questionGroupRelationList.size()));
            questionGroupDb.setUpdateTime(DateUtils.getNowDate());
            questionGroupDb.setCreateBy(SecurityUtils.getUsername());
            i = questionGroupMapper.updateQuestionGroup(questionGroupDb);
        }
        return i;
    }
}
